package cn.circle.system.dao.impl;

import java.util.List;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;
import cn.circle.commom.dao.impl.BaseDaoImpl;
import cn.circle.system.dao.ISysDepartmentDao;
import cn.circle.system.model.SysCustomer;
import cn.circle.system.model.SysDepartment;

@Repository
public class SysDepartmentDao extends BaseDaoImpl<SysDepartment> implements ISysDepartmentDao{

	@SuppressWarnings("unchecked")
	@Override
	public List<SysDepartment> datagrid() {
		// TODO Auto-generated method stub
		String hql = "from SysDepartment ";
		Query query = super.sessionFactory.getCurrentSession().createQuery(hql);
		return query.list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SysDepartment> getUser(String id) {
		// TODO Auto-generated method stub
		String hql = "from SysDepartment where depId = '"+id+"'";
		
		Query query = super.sessionFactory.getCurrentSession().createQuery(hql);
		return query.list();
	}

	@Override
	public boolean removeAll(String depIds) {
		String hql = "update SysDepartment s set s.status = 0 where s.depId in ("+depIds+") ";
		System.out.println(hql);
		Query query = super.sessionFactory.getCurrentSession().createQuery(hql);
		return (query.executeUpdate() > 0);
	}

	@Override
	public boolean deleteAll(String depIds) {
		// TODO Auto-generated method stub
		String hql = "delete SysDepartment s where s.depId in ("+depIds+") ";
		System.out.println(hql);
		Query query = super.sessionFactory.getCurrentSession().createQuery(hql);
		return (query.executeUpdate() > 0);
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<String> getFilePath(String depIds){
		String hql = "select depIcon from SysDepartment s where s.depId in ("+depIds+") ";
		System.out.println(hql);
		Query query = super.sessionFactory.getCurrentSession().createQuery(hql);
		List <String>  s= query.list();
		System.out.println(s);
		return s;
	}
	
	
	public boolean updateIt(SysDepartment c) {
		
		StringBuffer hql = new StringBuffer(
				"update SysDepartment s set ");
		
		//拼接修改条件
		if(c.getDepName() != null && !"".equals(c.getDepName())){
			hql.append("s.depName = '"+c.getDepName()+"' ");
		}
		if(c.getStatus() != null && !"".equals(c.getStatus())){
			hql.append(", s.status = "+c.getStatus()+" ");
		}
		if(c.getDepIcon() != null && !"".equals(c.getDepIcon())){
			hql.append(", s.depIcon = '"+c.getDepIcon()+"' ");
		}

		hql.append("where s.depId ="+c.getDepId()+" ");
		Query query = super.sessionFactory.getCurrentSession().createQuery(hql.toString());
		return (query.executeUpdate() > 0);
	}

	@Override
	public List<SysDepartment> getAllDepaetment(Integer type, Integer depId) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer();
		
		hql.append(" FROM SysDepartment d WHERE d.status = 9 ");
		
		if(type != null &&  !"".equals(type)){
			hql.append(" AND d.depType = "+type.byteValue()+"");
		}
		
		/*if(depId != null && !"".equals(depId)){
			hql.append("  AND FIND_IN_SET(d.depId,getChildDepId("+depId+")) > 0 ");
		}*/
		
		Query query = super.sessionFactory.getCurrentSession().createQuery(hql.toString());
		return  query.list();
	}
}
